/*
 * @Description: 用户服务
 * @Author: seadon
 * @LastEditors: seadon
 * @Date: 2021-07-21 20:55:33
 * @LastEditTime: 2021-07-21 21:37:03
 */
import { ref, Ref } from 'vue'
import { apiInit } from '@/services'

export default function useModel() {
	const api = apiInit()
	const countData: Ref<typing.TaskCountBo> = ref({})
	const finishCountData = ref({})
	const finishDealCountData = ref({})
	const userNotifyRows = ref({})
	const userNotifyAllRows = ref({})
	const userNotifyNoReadTotal = ref(0)
	const userNotifyReadTotal = ref(0)
	const userNotifyTotal = ref(0)
	const userNotifyUpdateCode = ref(0)
	const notifyLoading = ref<boolean>(false)
	const userNotifyColumns = ref([
		{
			title: '标题',
			dataIndex: 'title',
			key: 'title',
		},
		{
			title: '表单名称',
			dataIndex: 'formName',
			key: 'formName',
		},
		{
			title: '创建人',
			dataIndex: 'createBy',
			key: 'createBy',
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			key: 'createTime',
		},{
			title: '状态',
			dataIndex: 'isRead',
			key: 'isRead',
		},
		{
			title: '操作',
			dataIndex: 'operation',
		},
	])

	const fetchUserNotify = async (
	  pageNum: any,
		pageSize: any,
		userId: any,
		isRead: any
	) => {
		const { code, rows, total } = await api.default.UserNotifyData({
			payload: {
				pageNum: pageNum,
				pageSize: pageSize,
				userId: userId,
				isRead: isRead,
			},
		})
		if (code === 200 && rows) {
			userNotifyRows.value = rows
			if (total != null && isRead === 0) {
				userNotifyNoReadTotal.value = total
			}
			if (total != null && isRead === 1) {
				userNotifyReadTotal.value = total
			}
		}
	}

	const updateUserNotify = async (
		id: any,
		isRead: any,
	) => {
		const { code } = await api.default.UserNotifyUpdate({
			payload: {
				id: id,
				isRead: isRead,
			},
		})
		if (code) {
			userNotifyUpdateCode.value = code
		}
	}


	const fetchUserAllNotify = async (
		pageNum: any,
		pageSize: any,
		userId: any,
		isRead: any
	) => {
		notifyLoading.value = true
		const { code, rows, total } = await api.default.UserNotifyData({
			payload: {
				pageNum: pageNum,
				pageSize: pageSize,
				userId: userId,
				isRead: isRead,
			},
		})
		if (code === 200 && rows) {
			userNotifyAllRows.value = rows
			if (total != null) {
				userNotifyTotal.value = total
			}
			notifyLoading.value = false
		}
	}

	const fetchTaskCount = async () => {
		const { code, data } = await api.default.CountTaskPrivateV2POST({})
		if (code === '000000' && data) {
			countData.value = data
		}
	}

	const finishDealCount = async () => {
		const { code, data } = await api.default.CountByTypeDoneTaskPrivateV2POST({})
		if (code === '000000' && data) {
			finishDealCountData.value = data
		}
	}

	const finishCount = async (
		create_time_end: any,
		create_time_start: any,
		status: any,
		subject: any,
		time_sort: any,
		type_key_list: any
	) => {
		const { state, data } = await api.default.CountApplicationTaskPrivateV2POST(
			{
				payload: {
					create_time_end,
					create_time_start,
					status,
					subject,
					time_sort,
					type_key_list,
				},
			}
		)
		if (state === 'SUCCESS' && data) {
			finishCountData.value = data
		}
	}

	return {
		finishDealCountData,
		finishDealCount,
		finishCountData,
		finishCount,
		countData,
		fetchTaskCount,
		userNotifyRows,
		userNotifyNoReadTotal,
		userNotifyReadTotal,
		fetchUserNotify,
		fetchUserAllNotify,
		userNotifyAllRows,
		userNotifyTotal,
		userNotifyColumns,
		updateUserNotify,
		notifyLoading,
		userNotifyUpdateCode,
	}
}
